package com.nineclock.approve.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * feign拦截器， 在feign远程调用时 自动执行apply方法
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //获取pc访问 考勤微服务 的 请求对象
        //RequestContextHolder 可以获取请求的上下文对象
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
        //获取请求对象
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //获取请求头中的认证信息
        //request.getHeader("Authorization")
        //（feign请求对象）可以在feign请求头中添加 认证信息
        template.header("Authorization", request.getHeader("Authorization"));
    }
}
